/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pesqRaca.java
 *
 * Created on 24/01/2010, 15:33:59
 */

package cparc;

import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author danilo
 */
public class pesqRaca extends javax.swing.JInternalFrame {

     static cadastroAnim frameRaca;


    public void setRaca(cadastroAnim fRaca){
       frameRaca = fRaca;
    }
    /** Creates new form pesqRaca */
    public pesqRaca() {
        initComponents();

         tfConsulta.requestFocusInWindow();
         tfConsulta.selectAll();

        DefaultTableModel modelo = (DefaultTableModel) tbRaca.getModel();
        modelo.setRowCount(0);
        Statement stm = null;
        String consulta = "SELECT RCOD, RNOM FROM CPARC.CAD_RACAS ORDER BY RCOD ASC";
        int i=0;
        try  {
            stm = (Statement) conexao.getConnection(true).createStatement();
            ResultSet rs = stm.executeQuery(consulta);

            while(rs.next()){
                modelo.addRow(new String[] {Integer.toString(rs.getInt("RCOD")), rs.getString("RNOM")});
                modelo.fireTableCellUpdated(i, 0);
                modelo.fireTableCellUpdated(i, 1);
                i++;
            }

        } catch(Exception e){
            e.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btConsulta = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbRaca = new javax.swing.JTable();
        tfConsulta = new javax.swing.JTextField();

        btConsulta.setText("Consultar");
        btConsulta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConsultaActionPerformed(evt);
            }
        });

        tbRaca.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Raça"
            }
        ));
        tbRaca.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbRacaKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbRaca);

        tfConsulta.setText("Digite o Código da Raça");
        tfConsulta.setToolTipText("Pesquisa de Animais");
        tfConsulta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfConsultaKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 394, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(16, Short.MAX_VALUE)
                        .addComponent(tfConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btConsulta)
                        .addGap(27, 27, 27))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 292, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btConsulta))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConsultaActionPerformed

        DefaultTableModel modelo = (DefaultTableModel) tbRaca.getModel();
        modelo.setRowCount(0);
        Statement stm = null;
        String rcod = tfConsulta.getText();
        String consulta = null;

        if(!rcod.isEmpty()){
            consulta = "SELECT RCOD, RNOM FROM CPARC.CAD_RACAS WHERE RCOD =" + rcod;
        }else if (rcod.isEmpty() || rcod.equals("Digite o código da Raca")){
            consulta = "SELECT RCOD, RNOM FROM CPARC.CAD_RACAS ORDER BY RCOD ASC";
            tfConsulta.requestFocusInWindow();
            tfConsulta.selectAll();
        }
        System.out.println(consulta);
        int i=0;
        try  {
            stm = (Statement) conexao.getConnection(true).createStatement();
            ResultSet rs = stm.executeQuery(consulta);

            while(rs.next()){
                modelo.addRow(new String[] {Integer.toString(rs.getInt("RCOD")), rs.getString("RNOM")});
                modelo.fireTableCellUpdated(i, 0);
                modelo.fireTableCellUpdated(i, 1);
                i++;
            }

        } catch(Exception e){
            e.printStackTrace();
        }


        tbRaca.requestFocusInWindow();
}//GEN-LAST:event_btConsultaActionPerformed

    private void tbRacaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbRacaKeyPressed

        if(evt.getKeyCode() == 10 ){

            this.dispose();
            frameRaca.requestFocusInWindow();
            frameRaca.raca((String) tbRaca.getValueAt(tbRaca.getSelectedRow(), 0), (String) tbRaca.getValueAt(tbRaca.getSelectedRow(), 1));
        }
}//GEN-LAST:event_tbRacaKeyPressed

    private void tfConsultaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfConsultaKeyPressed

        if(evt.getKeyCode() == 10){

            btConsulta.doClick();
        }

        else if(evt.getKeyCode() == 27){

            this.dispose();
            frameRaca.requestFocusInWindow();
            frameRaca.escRaca();
        }
}//GEN-LAST:event_tfConsultaKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private static javax.swing.JButton btConsulta;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JTable tbRaca;
    public javax.swing.JTextField tfConsulta;
    // End of variables declaration//GEN-END:variables

}
